////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//		File Name		:		CPickup.h
//
//		Author			:		Jayson Wu
//
//		Purpose			:		Header to contain all generic functionality of the Pickup and its childeren
//
//		Dates			:		Written( Feb 13, 2011 ) by Jayson Wu		LastUpdate( Feb 13, 2011  ) by Jayson Wu 
//	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "CPickup.h"

#include "CPlatform.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "VIN_ObjectManager.h"
#include "CPlayer.h"


CPickup::CPickup()
{
	// Use PICK_PLAT for objects you can pick up AND platform on
	SetPickupType(PICK_NORMAL);
	SetType(OBJ_PICK);
}

CPickup::~CPickup()
{
}


void CPickup::Render()
{
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
	pTM->Draw( GetImageID(), (int)GetPosX(), (int)GetPosY(), (float)((float)GetWidth()/(float)pTM->GetTextureWidth(GetImageID())), (float)((float)GetHeight()/(float)pTM->GetTextureHeight(GetImageID())));

	RECT Collision;
	SetRect( &Collision, (int)GetPosX(), (int)GetPosY(), (int)(GetPosX() + GetWidth()), (int)(GetPosY() + GetHeight()) );
}


void CPickup::Update( float fElapsedTime )
{
	if(GetVelX() < 0)
		SetVelX(GetVelX() + 1);
	else if(GetVelX() > 0)
		SetVelX(GetVelX() -1);

	if(GetPosY() < 450)
		SetVelY(GetVelY() + 1);
	else if(GetPosY() > 450)
	{
		SetVelX(GetVelX() / 2);
		SetVelY(0);
	}
	CBaseObject::Update(fElapsedTime);
}

RECT CPickup::GetRect()
{
	RECT CollisionRect;
	SetRect(&CollisionRect, (int)GetPosX(), (int)GetPosY(), (int)(GetPosX() + GetWidth()), (int)(GetPosY() + GetHeight()));
	return CollisionRect;
}


bool CPickup::CheckCollision( IBaseInterface* pBase )
{
	RECT TempRect;

	if(IntersectRect(&TempRect, &GetRect(), &pBase->GetRect() ) )
	{
		if( pBase->GetType() == OBJ_PLAYER )
		{	
			CPlayer* pPBase = (CPlayer*)pBase;

			if(GetPickupType() == PICK_PLAT)
			{
				// If width is greater than height, then it is a Top/Bottom collision
				if(TempRect.bottom - TempRect.top < TempRect.right - TempRect.left)
				{
					// Top Of ledge Collision
					if((pPBase->GetPosY() < GetPosY()) && (pPBase->GetPosY() < (GetPosY() + GetHeight()/4)))
					{
						pPBase->SetPosY(GetPosY() - pPBase->GetHeight() - 1);
						pPBase->SetVelY(0);
						pPBase->Jumpy();
					}
				}
			}
		}
	}
	return false;
}
